<?php

Yii::import('application.extensions.PHPMailer.AqSendMail');

class CollegeController extends GxController {

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id, 'College'),
        ));
    }

//    public function actionCollgeInfo() {
//        $model = new College;
//        if (isset($_POST['College'])) {
//            $model->attributes = $_POST['College'];
//            if ($model->validate()) {
//                $model->save();
//                $adminSubject = 'Password for Update College information';
//                $adminTpl = 'sendCollegePassword.php';
//                $this->sendEmail($_POST['College']['hod_first_name'], $password, $_POST['College']['hod_email'], $adminSubject, $adminTpl);
//                $this->render('_thankyou');
//            }
//        }
//
//        $this->render('_create', array(
//            'model' => $model,
//        ));
//    }


    public function actionCollgeInfo() {
        $model = new College;
        $pass = '';
        if (isset($_POST['College'])) {
            $model->attributes = $_POST['College'];
            $pass = 1;
             $password = rand(substr($_POST['College']['hod_mobile_number'], 4, 7),3);
             $model->setAttribute('password', $password);
            if ($model->validate()) {
                $model->save();
                Yii::app()->user->setFlash('Password','Please check your mail and mobile no.i have send a password.');               
                Yii::app()->session['collge_id'] = $model->id;
                $adminSubject = 'Password for Update College information';
                $adminTpl = 'sendCollegePassword.php';
                $this->sendEmail($_POST['College']['hod_first_name'], $password, $_POST['College']['hod_email'], $adminSubject, $adminTpl);
                $this->render('_password_text');
            }
        }

        $this->render('_createCollege', array(
            'model' => $model, 'pass' => $pass
        ));
    }

    public function actionpassword() {
        if (!empty(Yii::app()->session['collge_id'])) {
            $model = $this->loadModel(Yii::app()->session['collge_id'], 'College');
            $this->render('_collegePassword', array(
                'model' => $model,
            ));
        }
    }

    public function actioncheckPassword() {
        if (!empty($_POST['password'])) {
            $password = College::model()->findByAttributes(array('id' => Yii::app()->session['collge_id'], 'password' => $_POST['password']));
            if (count($password) > 0) {
                echo 1;die;
            } else {
                echo 'Please enter currect password.';
            }
        }
    }

    public function actionUpdateCollgeInfor() {
        if (isset($_POST['College']['hod_first_name']) AND ! empty($_POST['College']['hod_first_name'])) {
            $password = substr($_POST['College']['hod_first_name'], 0, 2) . substr($_POST['College']['hod_email'], 3, 5) . substr($_POST['College']['hod_mobile_number'], 4, 7);
            College::model()->updateByPk(Yii::app()->session['collge_id'], array("hod_title" => $_POST['College']['hod_title'],
                "hod_first_name" => $_POST['College']['hod_first_name'],
                "hod_last_name" => $_POST['College']['hod_last_name'],
                "hod_email" => $_POST['College']['hod_email'], "hod_mobile_number" => $_POST['College']['hod_mobile_number'], 'password' => $password));
            $adminSubject = 'Password for Update College information';
            $adminTpl = 'sendCollegePassword.php';
            $this->sendEmail($_POST['College']['hod_first_name'], $password, $_POST['College']['hod_email'], $adminSubject, $adminTpl);
            echo "Please check your mail.your password has been send on mail and your mobile no. ";
        }
    }

    public function actionCollgeInfoUpdate() {
        if (!empty(Yii::app()->session['collge_id'])) {
            $model = $this->loadModel(Yii::app()->session['collge_id'], 'College');
            if (isset($_POST['College'])) {
                $model->attributes = $_POST['College'];
                if ($model->validate()) {
                    $model->save();
                    Yii::app()->session['collge_id'] = '';
                    $this->render('_thankyou');
                }
            }

            $this->render('update', array(
                'model' => $model,
            ));
        } else {
            throw new CHttpException(404, 'The specified request cannot be found.');
        }
    }

    public function actionCollgeInfoDetails() {
        if (!empty(Yii::app()->session['collge_id'])) {
            $model = $this->loadModel(Yii::app()->session['collge_id'], 'College');
            $this->render('_view', array(
                'model' => $model,
            ));
        } else {
            throw new CHttpException(404, 'The specified request cannot be found.');
        }
    }

    public function actionfindCollege() {
        $select = '';
        $data = College::model()->findAllByAttributes(array('state_id' => (int) $_POST ['State']['id']));
        $data = CHtml::listData($data, 'id', 'college_name');
        if (empty($data)) {
            $select = CHtml::tag('option', array('value' => ''), 'Select collge name', true);
        }
        foreach ($data as $value => $name) {
            $select.= CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
        }
        echo $select;
        die;
    }

    public function actioncollgeAddress() {
        $select = '';
        $data = College::model()->findAllByAttributes(array('id' => (int) $_POST ['College']['id']));
        $data = CHtml::listData($data, 'id', 'college_address');
        if (empty($data)) {
            $select = CHtml::tag('option', array('value' => ''), 'Select collge address', true);
        } else {
            foreach ($data as $value => $name) {
                $select.= CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
            }
        }
        echo $select;
        die;
    }

    public function actionDelete($id) {
        if (Yii::app()->getRequest()->getIsPostRequest()) {
            $this->loadModel($id, 'College')->delete();

            if (!Yii::app()->getRequest()->getIsAjaxRequest())
                $this->redirect(array('admin'));
        } else {
            throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
        }
    }

    public function actionIndex() {
        $model = new College;
        if (isset($_POST['State']['id'])) {
            $college = College::model()->findByAttributes(array('state_id' => $_POST['State']['id'], 'id' => $_POST['College']['id']));
            if (!empty($college)) {
                Yii::app()->session['collge_id'] = $college->id;
                $this->redirect(array('/College/CollgeInfoDetails'));
            } else {
                Yii::app()->user->setFlash('noresult', 'No record found.please click on i can not found link and create a collge.');
            }
        }
        $this->render('_college_search', array(
            'model' => $model,
        ));
    }

    public function actionAdmin() {
        $model = new College('search');
        $model->unsetAttributes();

        if (isset($_GET['College']))
            $model->setAttributes($_GET['College']);

        $this->render('admin', array(
            'model' => $model,
        ));
    }

    public function sendEmail($userName, $password, $to, $subject, $tpl) {
        $emalDir = aqConfig::getEmailDir();
        $mail['to'] = $to;
        $mail['subject'] = $subject;
        $mail['msgtpl'] = $emalDir . $tpl;
        $mailReplaceArr = array('$USER_NAME' => $userName, '$PASSWORD' => $password);

        AqSendMail::SendMail($mail, $mailReplaceArr);
    }

}
